﻿package com.sgcc.nfc.lock.presentation.warnings

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.google.android.material.snackbar.Snackbar
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.FragmentWarningDetailBinding
import com.sgcc.nfc.lock.domain.model.WarningStatus
import com.sgcc.nfc.lock.domain.model.WarningType
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WarningDetailFragment : Fragment() {

    private var _binding: FragmentWarningDetailBinding? = null
    private val binding get() = _binding!!
    private val viewModel: WarningDetailViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentWarningDetailBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.handleButton.setOnClickListener {
            viewModel.handleWarning(binding.remarkInput.text?.toString())
        }
        observeViewModel()
    }

    private fun observeViewModel() {
        viewModel.warning.observe(viewLifecycleOwner) { warning ->
            if (warning != null) {
                binding.warningDetailType.text = when (warning.warningType) {
                    WarningType.FORCED_OPEN -> getString(R.string.warning_type_forced_open)
                    WarningType.TIMEOUT -> getString(R.string.warning_type_timeout)
                    WarningType.BATTERY_LOW -> getString(R.string.warning_type_battery_low)
                    WarningType.MALFUNCTION -> getString(R.string.warning_type_malfunction)
                    WarningType.UNKNOWN -> getString(R.string.warning_type_unknown)
                }
                binding.warningDetailContent.text = warning.warningContent ?: "--"
                binding.warningDetailStatus.text = when (warning.status) {
                    WarningStatus.PENDING -> getString(R.string.warning_status_pending_label)
                    WarningStatus.HANDLED -> getString(R.string.warning_status_handled_label)
                    WarningStatus.CLOSED -> getString(R.string.warning_status_closed_label)
                    WarningStatus.UNKNOWN -> getString(R.string.warning_status_unknown_label)
                }
                binding.warningDetailTime.text = warning.createTime ?: "--"
            }
        }
        viewModel.isLoading.observe(viewLifecycleOwner) { binding.warningDetailLoading.isVisible = it }
        viewModel.message.observe(viewLifecycleOwner) { message ->
            message?.let { Snackbar.make(binding.root, it, Snackbar.LENGTH_LONG).show() }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

